/*
 * Login.java
 *
 * Created on 7 de enero de 2008, 11:41
 */

package vista;

import Datos.Consulta;
import controlador.Encriptar;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import negocios.Usuario;

/**
 *
 * @author  Administrador
 */
public class Login extends javax.swing.JFrame
{
    private int intentos = 0;
    
    /** Creates new form Login */
    public Login()
    {
        initComponents();
        // Codigo necesario para centrar la ventana
                
                Dimension pantalla, cuadro;
		pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		cuadro = this.getSize();
		this.setLocation(((pantalla.width - cuadro.width)/2), (pantalla.height - cuadro.height)/2);
                
                //Fin del centrado
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPClave = new javax.swing.JPasswordField();
        jTUsuario = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login");
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jPClave.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPClaveKeyPressed(evt);
            }
        });

        jTUsuario.setNextFocusableComponent(jPClave);
        jTUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTUsuarioActionPerformed(evt);
            }
        });
        jTUsuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTUsuarioKeyPressed(evt);
            }
        });

        jLabel1.setText("Usuario");
        jLabel1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jLabel1KeyPressed(evt);
            }
        });

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Clave");
        jLabel2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jLabel2KeyPressed(evt);
            }
        });

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButton1KeyPressed(evt);
            }
        });

        jButton2.setText("Salir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButton2KeyPressed(evt);
            }
        });

        jLabel3.setBackground(new java.awt.Color(255, 255, 255));
        jLabel3.setFont(new java.awt.Font("Arial Narrow", 1, 12));
        jLabel3.setForeground(new java.awt.Color(0, 51, 51));
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ctj50x53.gif"))); // NOI18N
        jLabel3.setText("          COLEGIO DE TECNICOS DE JUJUY");
        jLabel3.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPClave)
                            .addComponent(jTUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPClave, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButton2KeyPressed
    // TODO add your handling code here:
    if(evt.getKeyCode()==27)
        this.dispose();
}//GEN-LAST:event_jButton2KeyPressed

private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButton1KeyPressed
    // TODO add your handling code here:
    if(evt.getKeyCode()==27)
        this.dispose();
    if(evt.getKeyCode() == 10)
        this.login();
}//GEN-LAST:event_jButton1KeyPressed

private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
    // TODO add your handling code here:
    if(evt.getKeyCode()==27)
        this.dispose();
}//GEN-LAST:event_formKeyPressed

private void jPClaveKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPClaveKeyPressed
    // TODO add your handling code here:
    if(evt.getKeyCode()==27)
        this.dispose();
    if(evt.getKeyCode()==10)
        this.login();
}//GEN-LAST:event_jPClaveKeyPressed

private void jLabel2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jLabel2KeyPressed
    // TODO add your handling code here:
    if(evt.getKeyCode()==27)
        this.dispose();
}//GEN-LAST:event_jLabel2KeyPressed

private void jLabel1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jLabel1KeyPressed
    // TODO add your handling code here:
    if(evt.getKeyCode()==27)
        this.dispose();
}//GEN-LAST:event_jLabel1KeyPressed

private void jTUsuarioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTUsuarioKeyPressed
    // TODO add your handling code here:
    if(evt.getKeyCode()==27)
        this.dispose();
    if(evt.getKeyCode()==10)
        this.jPClave.requestFocus();
}//GEN-LAST:event_jTUsuarioKeyPressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
    {//GEN-HEADEREND:event_jButton2ActionPerformed
// TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
// TODO add your handling code here:
        
        this.login();
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTUsuarioActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jTUsuarioActionPerformed
    {//GEN-HEADEREND:event_jTUsuarioActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_jTUsuarioActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new Login().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jPClave;
    private javax.swing.JTextField jTUsuario;
    // End of variables declaration//GEN-END:variables
 
    private boolean control()
    {
        boolean bandera = true;
        if(this.jTUsuario.getText().trim().length() == 0)
            bandera = false;
        if(String.valueOf(this.jPClave.getPassword()).trim().length() == 0)
            bandera = false;
        if(intentos == 3)
            this.dispose();
        return bandera;
    }
    
    
    private void login()
    {
        
        if(this.control())
        {
            /*if(this.jTUsuario.getText().trim().toUpperCase().equals("ADMIN"))
                if(String.valueOf(this.jPClave.getPassword()).trim().toUpperCase().equals("ADMIN"))
                {
                    UIMenu menu = new UIMenu();
                    menu.setVisible(true);
                    this.dispose();
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"Nombre de usuario o clave incorrecta","Usuario invalido clave",JOptionPane.ERROR_MESSAGE);
                    this.jTUsuario.setText("");
                    this.jPClave.setText("");
                    intentos++;
                    boolean a = this.control();
                    this.jTUsuario.requestFocus();
                }
            else*/
                this.validarUsuario();
        }
        else
        {
            JOptionPane.showMessageDialog(null,"Debe ingresar el nombre de usuario y la clave","Faltan datos",JOptionPane.ERROR_MESSAGE);
            this.jTUsuario.setText("");
            this.jPClave.setText("");
            this.jTUsuario.requestFocus();
        }
    }
    
    
    
    private void validarUsuario()
    {
        String clave = "";
        clave = String.valueOf(this.jPClave.getPassword());
        Usuario usuario = new Usuario();
        usuario = this.getUsuario();
        if(usuario.getNombre() == null)
        {
            JOptionPane.showMessageDialog(null,"Nombre de usuario o clave incorrecta","Usuario invalido clave",JOptionPane.ERROR_MESSAGE);
            this.jTUsuario.setText("");
            this.jPClave.setText("");
            intentos++;
            boolean a = this.control();
            this.jTUsuario.requestFocus();
        }
        else
        {
            clave = this.encriptar(clave);
            if(clave.equals(usuario.getPassword().trim()))
            {
                UIMenu menu = new UIMenu(usuario);
                menu.setVisible(true);
                this.dispose();
            }
            else
            {
                JOptionPane.showMessageDialog(null,"Nombre de usuario o clave incorrecta","Usuario invalido clave",JOptionPane.ERROR_MESSAGE);
                this.jTUsuario.setText("");
                this.jPClave.setText("");
                intentos++;
                boolean a = this.control();
                this.jTUsuario.requestFocus();
            }
        }
    }
    
    private Usuario getUsuario()
    {
        Consulta consulta = new Consulta();
        Usuario usuario = new Usuario();
        usuario = consulta.getUser(this.jTUsuario.getText().toUpperCase().trim());
        return usuario;
    }
    
    private String encriptar(String clav)
    {
        String c = "";
        Encriptar enc = new Encriptar();
        c = enc.codificar(clav.trim().toUpperCase());
        return c;
    }
}
